// pages/plan/plan.js
Page({
  data: {
    currentDate: '',
    planList: [
      {
        id: 1,
        name: '早餐',
        time: '08:00',
        foods: ['鸡蛋', '牛奶', '面包'],
        completed: true
      },
      {
        id: 2,
        name: '午餐',
        time: '12:30',
        foods: ['米饭', '红烧肉', '青菜'],
        completed: false
      },
      {
        id: 3,
        name: '晚餐',
        time: '18:30',
        foods: ['面条'],
        completed: false
      }
    ],
    showAddModal: false,
    newPlan: {
      name: '',
      time: '',
      foods: ''
    }
  },

  onLoad() {
    console.log('饮食计划页面加载');
    this.setCurrentDate();
  },

  onShow() {
    // 页面显示时的逻辑
  },

  // 设置当前日期
  setCurrentDate() {
    const now = new Date();
    const dateStr = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, '0')}-${String(now.getDate()).padStart(2, '0')}`;
    this.setData({
      currentDate: dateStr
    });
  },

  // 切换计划完成状态
  togglePlanStatus(e) {
    const index = e.currentTarget.dataset.index;
    const planList = this.data.planList;
    planList[index].completed = !planList[index].completed;
    
    this.setData({
      planList: planList
    });
    
    wx.showToast({
      title: planList[index].completed ? '已完成' : '已取消',
      icon: 'success'
    });
  },

  // 显示添加计划弹窗
  showAddPlanModal() {
    this.setData({
      showAddModal: true
    });
  },

  // 隐藏添加计划弹窗
  hideAddPlanModal() {
    this.setData({
      showAddModal: false,
      newPlan: {
        name: '',
        time: '',
        foods: ''
      }
    });
  },

  // 输入框变化
  onInput(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    
    this.setData({
      [`newPlan.${field}`]: value
    });
  },

  // 添加计划
  addPlan() {
    const { name, time, foods } = this.data.newPlan;
    
    if (!name || !time || !foods) {
      wx.showToast({
        title: '请填写完整信息',
        icon: 'error'
      });
      return;
    }
    
    const newPlan = {
      id: this.data.planList.length + 1,
      name: name,
      time: time,
      foods: foods.split(',').map(item => item.trim()),
      completed: false
    };
    
    this.setData({
      planList: [...this.data.planList, newPlan],
      showAddModal: false,
      newPlan: {
        name: '',
        time: '',
        foods: ''
      }
    });
    
    wx.showToast({
      title: '添加成功',
      icon: 'success'
    });
  },

  // 删除计划
  deletePlan(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个计划吗？',
      success: (res) => {
        if (res.confirm) {
          const planList = this.data.planList.filter(plan => plan.id !== id);
          this.setData({
            planList: planList
          });
          
          wx.showToast({
            title: '删除成功',
            icon: 'success'
          });
        }
      }
    });
  },

  // 底部导航切换
  switchTab(e) {
    const tab = e.currentTarget.dataset.tab;
    
    switch (tab) {
      case 'home':
        wx.redirectTo({
          url: '/pages/index/index'
        });
        break;
      case 'history':
        wx.redirectTo({
          url: '/pages/history/history'
        });
        break;
      case 'plan':
        // 当前页面，无需跳转
        break;
      case 'profile':
        wx.redirectTo({
          url: '/pages/profile/profile'
        });
        break;
      default:
        wx.redirectTo({
          url: '/pages/index/index'
        });
    }
  }
})